package com.mxchip.livestarmobile.mobile.ui.contactdetail


import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import com.mxchip.livestarmobile.mobile.CallActivityA
import com.mxchip.livestarmobile.R
import com.mxchip.livestarmobile.mobile.ui.GroupCall
import com.mxchip.livestarmobile.mobile.ui.P2PSingleCall
import com.mxchip.livestarmobile.mobile.util.Constant
import com.mxchip.livestarmobile.mobile.base.toast
import kotlinx.android.synthetic.main.fragment_contact_detail.*


class ContactDetailFragment : Fragment() {

    private val contactDetailViewModel by viewModels<ContactDetailViewModel>()




    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        val root = inflater.inflate(R.layout.fragment_contact_detail, container, false)

        ViewCompat.setTranslationZ(root, -1f)

        return root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val uid = requireActivity().intent.getLongExtra("contactId", -1L)

        contactDetailViewModel.loadContact(uid)

        contactDetailViewModel.contact.observe(viewLifecycleOwner, Observer {

            if(it == null) {
                return@Observer
            }

            text_name.text = it.fullName
            text_hxb_number.text = it.hxbNumber
            text_mobile_number.text = it.mobileNumber

            text_mobile_type.text = "手机"

            if(it.hxbNumber?.length == Constant.LOCAL_CODE_LENGTH) {
                text_hxb_type.text = "会小宝终端"
            }

            // 手机号
            else if(it.hxbNumber?.length == 11) {
                text_hxb_type.text = "会小宝移动端"
            }

            else {
                text_hxb_type.text = "未知设备"
            }

            Log.e("Test", "it.mobileNumber: " + it.mobileNumber)

            if ( it.mobileNumber.isNullOrBlank() ) {
                group_phone_number.visibility = View.GONE
            } else {
                group_phone_number.visibility = View.VISIBLE
            }


        })


        view_hxb_number.setOnClickListener {

            val number = contactDetailViewModel.contact.value?.hxbNumber

            val call = contactDetailViewModel.makeCall(number!!)
            val callString = when (call) {
                is P2PSingleCall ->
                    call.number
                is GroupCall ->
                    call.groupNumber

                else ->
                    throw IllegalStateException("call is Unknow type")
            }
            contactDetailViewModel.insertCallLog()
            context?.let { CallActivityA.open(it, call) }

        }

        view_mobile_number.setOnClickListener {

        }

        view_edit.setOnClickListener {

            val action = ContactDetailFragmentDirections.actionContactDetailFragmentToAddMemberFragment()

            action.id = contactDetailViewModel.contact.value?.uid!!
            action.number = contactDetailViewModel.contact.value?.hxbNumber
            action.name = contactDetailViewModel.contact.value?.fullName

            findNavController().navigate(action)
        }

        view_delete.setOnClickListener {
            contactDetailViewModel.deleteContact()

            toast("删除成功")
            activity?.finish()
        }
    }

}
